package com.yfw.maple_leaf_store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfw.maple_leaf_store.common.BaseContext;
import com.yfw.maple_leaf_store.common.R;
import com.yfw.maple_leaf_store.domain.Goods;
import com.yfw.maple_leaf_store.domain.ShoppingCart;
import com.yfw.maple_leaf_store.domain.User;
import com.yfw.maple_leaf_store.dto.GoodsDto;
import com.yfw.maple_leaf_store.service.GoodsService;
import com.yfw.maple_leaf_store.service.ShoppingCartService;
import com.yfw.maple_leaf_store.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Arg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.sampled.Line;
import java.util.List;
import java.util.Map;
import java.util.function.DoubleToIntFunction;
import java.util.stream.Collectors;

/**
 * @author: yfw
 * @date: 2022/6/16 23:40
 * com.yfw.maple_leaf_store.controller
 */
@RestController
@Slf4j
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;
    /**
     * 添加商品
     * @param goods
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Goods goods){
        Long currentId = BaseContext.getCurrentId();//获取当前用户id
        //Long currentId = Long.valueOf("1537370935432450049");//测试
        //添加商品
        goods.setSid(currentId);
        goodsService.save(goods);
        return R.success("添加成功");
    }

    /**
     *
     * @param id 商品id
     * @return
     */
    @GetMapping("/myShell")
    public R<List<Goods>> myShell(Long id){
        Long currentId = BaseContext.getCurrentId();//获取当前用户id
//        Long currentId = Long.valueOf("1537370935432450049");//测试
        //根据商品卖家id查询所出售的商品
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getSid,currentId).eq(id != null && id > 0,Goods::getId,id);
        List<Goods> list = goodsService.list(wrapper);
        return R.success(list);
    }


    /**
     * @param current     当前所在页
     * @param size 每页最多显示记录
     * @param name     根据商品名称模糊查询
     * @param typeId   根据商品类型id查询
     * @return
     */
    @GetMapping("/list")
    public R<Page> list(int current, int size, String name, Long typeId) {
        //获取当前用户id
        //Long currentId = Long.valueOf("1537801628956426242");
        Long currentId = BaseContext.getCurrentId();

        log.info(current + "+" + size + "+" + name + "+" + typeId);
        //构造分页构造器对象
        Page<Goods> pageInfo = new Page<>(current, size);
        //为了将结果集呈现给页面而封装的结果集
        Page<GoodsDto> goodsDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<Goods>();
        queryWrapper.like(name != null && !"".equals(name), Goods::getName, name);
        queryWrapper.eq(typeId != null && typeId > 0, Goods::getCid, typeId);
        //执行分页查询
        Page<Goods> goodsPage = goodsService.page(pageInfo, queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,goodsDtoPage,"records");

        List<Goods> records = pageInfo.getRecords();
        //stream给每一个对象额外附加一个当前商品的加购数量
        List<GoodsDto> list = records.stream().map(item -> {
            //拷贝属性属性到对应的DTO对象
            GoodsDto goodsDto = new GoodsDto();
            BeanUtils.copyProperties(item,goodsDto);

            //获取当前商品id
            Long id = goodsDto.getId();
            //根据商品id查询对购物车数量
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            //根据用户id
            wrapper.eq(ShoppingCart::getUid,currentId).eq(ShoppingCart::getId,id);
            ShoppingCart cart = shoppingCartService.getOne(wrapper);
            if (cart != null){
                //将查询的加购数量赋值到结果中
                Integer number = cart.getNumber();
                goodsDto.setNumber(number);
            }
            LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(User::getId,goodsDto.getSid());
            User seller_user = userService.getOne(userWrapper);
            goodsDto.setSeller_name(seller_user.getName());
            return goodsDto;
        }).collect(Collectors.toList());
        goodsDtoPage.setRecords(list);
        return R.success(goodsDtoPage);
    }

    /**
     * 修改某个已出售的商品
     * @param goods
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Goods goods){
        log.info("{}",goods);
        LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(goods.getName() != null,Goods::getName, goods.getName())
        .set(goods.getCid() != null,Goods::getCid, goods.getCid())
        .set(goods.getPrice() != null, Goods::getPrice,goods.getPrice())
        .set(goods.getImage()!=null,Goods::getImage,goods.getImage())
        .eq(Goods::getId,goods.getId());
        goodsService.update(wrapper);
        return R.success("修改成功");
    }

}
